//
//  YZImageView.swift
//  Weibo
//
//  Created by 杨雨哲 on 15/7/6.
//  Copyright © 2015年 yyz. All rights reserved.
//

import UIKit

class YZImageView: UIImageView {
    
    var progress: CGFloat = 0 {
        didSet {
            progressView.progress = progress
        }
    }
    
    private lazy var progressView:YZProgressView = {
        [unowned self] in
        
        let prView = YZProgressView()
        
        self.addSubview(prView)
        
        prView.ff_Fill(self)
        
        return prView

    }()

    private class YZProgressView: UIView {
        
        var progress: CGFloat = 0 {
            didSet {
                setNeedsDisplay()
            }
        }
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            backgroundColor = UIColor.clearColor()
        }

        required init(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        private override func drawRect(rect: CGRect) {
            if progress == 0 || progress == 1 {
                return
            }
            
            let r = min(rect.width, rect.height) * 0.4
            let center = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
            let ovalRect = CGRect(x: center.x - r, y: center.y - r, width: r * 2, height: r * 2)
            
            let path1 = UIBezierPath(ovalInRect: ovalRect)
            UIColor(white: 1, alpha: 0.5).setFill()
            path1.fill()
            
            
            let start = CGFloat(-M_PI_2)
            let end = CGFloat(2 * M_PI) * progress + start
            let path2 = UIBezierPath(arcCenter: center, radius: r, startAngle:start, endAngle: end, clockwise: true)
            path2.addLineToPoint(center)
            path2.closePath()
            UIColor(white: 0, alpha: 0.5).setFill()
            path2.fill()
        }
        
    }

}
